package com.liuheng.manager.service.impl;

import com.liuheng.manager.mapper.SysRoleMenuMapper;
import com.liuheng.manager.service.SysMenuService;
import com.liuheng.manager.service.SysRoleMenuService;
import com.liuheng.manager.service.SysRoleService;
import com.liuheng.manager.utils.MenuHelper;
import com.liuheng.spzx.model.dto.system.AssginMenuDto;
import com.liuheng.spzx.model.entity.system.SysMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
public class SysRoleMenuServiceImpl implements SysRoleMenuService {
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;
    @Autowired
    private SysMenuService sysMenuService;
    @Override
    public Map<String, Object> findSysRoleMenuByRoleId(Long roleId) {
        List<SysMenu> sysMenuList = sysMenuService.list();
        List<SysMenu> tree = MenuHelper.getTree((ArrayList<SysMenu>) sysMenuList);
        List<Long> sysRoleMenuByRoleId = sysRoleMenuMapper.findSysRoleMenuByRoleId(roleId);
        Map<String, Object> map = new HashMap<>();
        map.put("sysMenuList",tree);
        map.put("roleMenuIds",sysRoleMenuByRoleId);
        return map;
    }

    @Override
    public void doAssign(AssginMenuDto assginMenuDto) {
        sysRoleMenuMapper.deleteByRoleId(assginMenuDto.getRoleId());
        sysRoleMenuMapper.save(assginMenuDto.getMenuIdList(),assginMenuDto.getRoleId());
    }
}
